package com.xzavier.yunlian_interview.utils;

import com.xzavier.yunlian_interview.service.ex.LoginException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authorization");
        if (token.isEmpty()) {
            throw new LoginException("缺失Token");
        }
        String userId = JwtUtils.verifyToken(token);
        if (userId.isEmpty()) {
            throw new LoginException("TOKEN解析失败");
        }
        request.setAttribute("userId", userId);
//        System.out.println(request.getAttribute("userId"));
        return true;
    }

}
